package com.functest.orm.experimental

import com.functest.orm.BaseProvider
import com.functest.orm.Condition

/**
 * @author: DavydovMD
 * Date: 06.09.13
 * Time: 12:51
 */
class CachingProvider<T> extends BaseProvider<T> {
    final BaseProvider<T> provider;
    
    CachingProvider(BaseProvider provider) {
        super(provider.clazz, provider.mapper)
        this.provider = provider
    }


    final private Closure<List<T>> clUsingFilters = {   List<Condition> filters ->
        provider.usingFilters (filters)
    }.memoizeAtMost(1000)

    @Override
    List<T> usingFilters(List<Condition> filters) {
        clUsingFilters(filters)
    }

    final private Closure<T> clByFieldValueFirst = {String field, Object value ->
        provider.byFieldValueFirst(field, value)
    }.memoizeAtMost(1000)

    @Override
    T byFieldValueFirst(String field, Object value) {
        clByFieldValueFirst(field,value)
    }


    final private Closure<T> clById = {def id ->
        provider.byId(id)
    }.memoizeAtMost(1000)

    @Override
    T byId(def id) {
         clById(id)
    }


}
